package cn.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.dto.Message;
import cn.dto.Result;
import cn.dto.common;
import cn.pojo.BlogList;
import cn.service.BlogListService;
import cn.service.impl.BlogListServiceImpl;

/**
 * Servlet implementation class addBlogServlet
 */
@WebServlet("/addBlogServlet")
public class addBlogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BlogListService blogListService = new BlogListServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数
		String postdata = common.getPostData(request.getInputStream(), request.getContentLength(), null);
		JSONObject addBlogparam = JSON.parseObject(postdata);
		String blogTitle = addBlogparam.getString("title");
		String blogContent = addBlogparam.getString("content");
		String blogAuthor = addBlogparam.getString("author");
		String blogCategroy = addBlogparam.getString("category");
		BlogList blogList = new BlogList();
		blogList.setTitle(blogTitle);
		blogList.setContent(blogContent);
		blogList.setAuthor(blogAuthor);
		blogList.setCategroy(blogCategroy);
		Boolean flag = blogListService.addBlog(blogList);
		Result result = new Result();
		if(flag == true) {
			Message message = new Message(200,"添加文章成功");
			result.setMessage(message);
		}else {
			Message message = new Message(400,"添加文章失败");
			result.setMessage(message);
		}
		response.getWriter().write(JSON.toJSONString(result));
	}

}
